% 交叉操作函数，使用单点交叉算法
function population_end = GA_crossoverMutation(population,crossoverRate,mutationRate)
    % 执行交叉操作
    % population 是当前种群
    % crossoverRate 是交叉概率
    % offspringPopulation 是经过交叉操作后产生的新种群
    
    popNum = size(population, 3); % 种群个数
    popSize = size(population,1); % 种群大小(种群个体长度)
    %indSize = size(population, 2); % 个体长度
    
    % 初始化新种群矩阵
    population_end = zeros(size(population));
    
    % 对种群中的每一对个体进行交叉操作
    numPairs = floor(popNum / 2);
    for i = 1:numPairs
        %if rand < crossoverRate % 根据交叉概率决定是否进行交叉
            % 选择两个父代个体
            parent1 = population(:,:,2*i-1);
            parent2 = population(:,:,2*i);
            %parents = population([2*i-1, 2*i], :);
            % 应用单点交叉
            for j = 1:1:popSize
                children_temp = GA_singlePointCrossoverAndMutation(parent1(j,:),parent2(j,:),crossoverRate,mutationRate);
                population_end(j,:,2*i-1) = children_temp(1,:);
                population_end(j,:,2*i) = children_temp(2,:);
            end
            %children = GA_singlePointCrossoverAndMutation(parents(1,:), parents(2,:),mutationRate);
            % 将子代加入到新种群中
            %population_end([2*i-1, 2*i], :) = children;
        %else
            % 直接复制父代到新种群
            %population_end([2*i-1, 2*i], :) = population([2*i-1, 2*i], :);
            %population_end(:,:,2*i-1) = population(:,:,2*i-1);
            %population_end(:,:,2*i) = population(:,:,2*i);
        %end
    end
end